package com.mmp.main.servlet;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.bo.Message.MESSAGE_TYPE;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.util.Default;
import com.mmp.main.util.WebUtil;

@WebServlet(urlPatterns = { "/ViewJobOrderReport" })
public class InquireJobOrderProcessServlet extends BasicServlet {

	private static HashMap<String, String> destinations;

	static {
		destinations = new HashMap<String, String>();
		destinations.put("ViewJobOrderReport", "ViewStandardJobOrderReport");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String orderNo = WebUtil.getParamValue(request, "jobOrderNo", null);
		int siteId = getSessionSiteId(request);
		int locationId = getSessionLocationId(request);

		JobOrder order = dbQueryManager.inquireJobOrder(siteId, locationId, orderNo);

		String msg = null;

		if (order == null)
			msg = "لم يتم العثور علي أي نتائج موافقة لبحثك";
		else if (order.getCancelled())
			msg = "أمر العمل المطلوب قد تم إلغاءه";

		if (msg != null) {
			if (WebUtil.getParamValue(request, "report", null) != null)
				announceErorrMessage(request, response, msg);
			else {
				setMessage(request, new Message(msg, MESSAGE_TYPE.ERROR));
				basicForward(request, response, WebUtil.getSearchDisplayURL(request));
			}
			return;
		}

		request.setAttribute(Default.JOB_ORDER_INFO_ATTR_NAME, order);
		request.setAttribute(Default.DEVICE_INFO_ATTR_NAME, order.getHospitalDevice());
		basicForward(request, response, getSearchDestination(destinations, request));

	}
}
